package cn.highset.modules.base.controller.manage;

import cn.highset.common.utils.PageUtil;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.vo.PageVo;
import cn.highset.common.vo.Result;
import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.entity.Video;
import cn.highset.modules.base.factory.VideoFactory;
import cn.highset.modules.base.service.VideoService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Victor
 */
@Slf4j
@RestController
@Api(description = "视频表管理接口")
@RequestMapping("/highset/video")
@Transactional
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value = "/getByCondition", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Video>> getByCondition(@ModelAttribute Video video,
                                              @ModelAttribute SearchVo searchVo,
                                              @ModelAttribute PageVo pageVo) {

        Page<Video> page = videoService.findByCondition(video, searchVo, PageUtil.initPageOrderMore(pageVo));
        return new ResultUtil<Page<Video>>().setData(page);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加视频")
    public Result<Object> add(@ModelAttribute Video o) {
        if (StrUtil.hasBlank(o.getTitle(), o.getImageUrl(), o.getVideoUrl())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }

        Video bean = videoService.save(o);
        if (bean == null) {
            return new ResultUtil<Object>().setErrorMsg("添加失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改视频", notes = "需要通过id获取原视频信息")
    public Result<Object> edit(@ModelAttribute Video o) {
        if (StrUtil.hasBlank(o.getTitle(), o.getImageUrl(), o.getVideoUrl())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }
        Video old = videoService.get(o.getId());

        Video bean = videoService.update(VideoFactory.edit(o, old));

        if (bean == null) {
            return new ResultUtil<Object>().setErrorMsg("修改失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过ids删除", notes = "逻辑删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "path", name = "ids", value = "视频ID数组", dataType = "String", required = true, allowMultiple = true),
    })
    public Result<Object> delAllByIds(@PathVariable String[] ids) {
        for (String id : ids) {
            videoService.delete(id);
        }

        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }


}
